<?php

class TuoiTre {

    var $links = array();
    var $title = "";
    var $intro = "";
    var $content = "";
    var $imgurl = "";

    function Init() {
        $this->title = "";
        $this->intro = "";
        $this->content = "";
        $this->imageurl = "";
    }

    function GetLink($link) {
        $link = trim($link);
        $document = TextBetween('<div style="background-image:url(Skin/Middle/CSS_dot.gif); background-position: left bottom; background-repeat:repeat-x; padding-bottom:5px;float:left">', '<li style="line-height: 24px; padding-left:12px; font-family:Arial; font-size:12px">', FetchDataString($link));

        $document = str_replace("amp;", "", $document);

        preg_match_all('/href=[\'"](Index\.aspx\?ArticleID=[0-9]+.+ChannelID=[0-9]+)[\'"]/i', $document, $result, PREG_PATTERN_ORDER);

        $links = array();

        $n = sizeof($result[1]);

        for ($i = 0; $i < $n; $i++) {
            $flag = 0;
            for ($j = $i + 1; $j < $n; $j++)
                if ($result[1][$j] == $result[1][$i])
                    $flag = 1;

            if ($flag == 0)
                array_push($this->links, 'http://www.tuoitre.com.vn/Tianyon/' . $result[1][$i]);
        }

        return true;
    }

    function GetInfo($link) {
        $this->Init();

        $document = Unicode_Decode(FetchDataString($link));

        $document = TextBetween('<div id="divContent" style="float:left; margin:0 0 0 0; padding:0px 0px 20px 0px;">', '<div style="width:100%; float:left; margin-top:10px; border-top:1px dashed #8F8F8F;">', $document);

        $search = array('/<IMG(.+)src="(ImageView\.aspx\?ThumbnailID=[0-9]+)"/i');
        $replace = array('<img src="http://www.tuoitre.com.vn/Tianyon/\2"');

        $document = preg_replace($search, $replace, $document);

        if (!preg_match('%<P class=pTitle.*>(.*?)</P>%i', $document, $result))
            return false;

        $this->title = trim($result[1]);

        if (!preg_match('/<img.*?src="(.*?ThumbnailID=[0-9]+)".*?>/i', $document, $regs))
            $this->imgurl = "";
        else
            $this->imgurl = $regs[1];

        $this->intro = TextBetween('<P class=pHead>', '</P>', $document);

        $p = strrpos($this->intro, "</TABLE>");

        if ($p)
            $this->intro = substr($this->intro, $p + 8);

        $this->content = substr($document, strpos($document, $this->intro) + strlen($this->intro));

        return true;
    }

}

?>